package com.example.classroom.controller;

import com.example.classroom.entity.Classroom;
import com.example.classroom.entity.UpdateClassroom;
import com.example.classroom.service.ClassroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/classroom")
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;

    @GetMapping("/getall")
    public List<Classroom> findAllClassroom() {
        return classroomService.findAllClassroom();
    }

    @PostMapping("/add")
    public ResponseEntity<Integer> addClassroom(@RequestBody Classroom classroom) {
        Integer result = classroomService.addClassroom(classroom);
        if (result != null) {
            return ResponseEntity.status(HttpStatus.CREATED).body(result); // 返回201创建成功的状态码
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); // 返回400错误的状态码
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> removeClassroom(@PathVariable int id) {
        try {
            int result = classroomService.removeClassroom(id);
            if (result > 0) {
                return ResponseEntity.ok("删除成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到该教室");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
        }
    }

    @PutMapping("/update/{id}")
    public ResponseEntity<String> updateClassroom(@PathVariable int id, @RequestBody UpdateClassroom updateclassroom) {
        try {
            int result = classroomService.updateClassroom(id, updateclassroom);
            if (result > 0) {
                return ResponseEntity.ok("更新成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到该教室");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("更新失败");
        }
    }

    @GetMapping("/search")
    public ResponseEntity<List<Classroom>> searchClassroom(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String building,
            @RequestParam(required = false) int capacity) {
        try {
            List<Classroom> result = classroomService.searchClassroom(name, building, capacity);
            if (result != null && !result.isEmpty()) {
                return ResponseEntity.ok(result);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); // 返回404未找到的状态码
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); // 返回500服务器错误的状态码
        }
    }

}
